package com.sdy.hospitalguard.h5.controller;

import com.sdy.common.model.Response;
import com.sdy.common.utils.EncodeUtil;
import com.sdy.hospitalguard.biz.constants.RedisConstants;
import com.sdy.hospitalguard.h5.config.WxConfig;
import com.sdy.mvc.controller.BaseController;
import com.sdy.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.KeyPair;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zzq on 2019/1/10.
 */
@Slf4j
@RequestMapping("/index")
@Api(tags = "基础信息接口")
@RestController
public class IndexController extends BaseController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private WxConfig wxConfig;

    @GetMapping("/publicKey")
    public Response<Object> publicKey(HttpServletRequest request) throws Exception {
        KeyPair keyPair = EncodeUtil.rsaGenKeyPair();
        String publicKeyStr = new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded()));
        redisService.set(RedisConstants.REDIS_PREFIX + "privateKey_" + getUserId(request), Base64.getEncoder().encode(keyPair.getPrivate().getEncoded()));
        return Response.success(publicKeyStr);
    }

    @GetMapping("/userInfo")
    public Response<Object> userInfo(HttpServletRequest request) {
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("userId", getUserId(request));
        return Response.success(userInfo);
    }

    @GetMapping("/oaAppId")
    @ApiOperation(value = "获取公众号APPID")
    public Response oaAppId() {
        return Response.success(wxConfig.getOaAppid());
    }
}
